var $projects = $('#projects .budget-list');
var $order = $('.budget__list--header [data-collection-sort-controls]');
var $projectsCount = $('#projects-count');
var $orderFilterInput = $('.order_filter');

$projects.html('<%= j(render partial: "projects_list").strip.html_safe %>');
$order.html('<%= j(render partial: "order").strip.html_safe %>');
$projectsCount.html('<%= j(render partial: "count").strip.html_safe %>');
$orderFilterInput.val('<%= order %>');

var $map = $("#map");
var controller = $map.data("map-controller");
if (controller) {
  var markerData = JSON.parse('<%= escape_javascript cell("decidim/map", all_geocoded_projects, metadata_card: "decidim/budgets/project_metadata").geocoded_data.to_json.html_safe %>');
  controller.clearMarkers();
  if (markerData.length > 0){
    controller.addMarkers(markerData);
  }
}

document.dispatchEvent(new CustomEvent("ajax:loaded", { detail: $projects[0] }));
